<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>订单表</title>
    <link rel="stylesheet" href="http://47.100.102.177:8080/app/css/common/bootstrap.min.css">
    <link rel="stylesheet" href="../../../boot/plugin/download/font_2801350_hw9pyy8c125/iconfont.css">    <script src="http://47.100.102.177:8080/app/js/common/jquery-1.12.4.js"></script>
    <!-- 必须先引入vue，  后使用element-ui -->
    <script src="../../../boot/plugin/element/vue.js"></script>
    <!--引入 element-ui 的样式，-->
    <link rel="stylesheet" href="../../../boot/plugin/element/element.css">
    <!-- 引入element 的组件库-->
    <script src="../../../boot/plugin/element/element.js"></script>
    <style>
        th {
            text-align: center;
        }
        
        .bg {
            width: 100%;
            height: 100vh;
            position: absolute;
            top: 0px;
            background-color: rgba(0, 0, 0, 0.2);
            display: none;
        }
        
        .inf {
            background-color: white;
            height: 630px;
            width: 60%;
            margin: auto;
            margin-top: 60px;
            border-radius: 3px;
        }
        
        .inf-head {
            width: 100%;
            height: 30px;
            background-color: rgb(21, 184, 215);
            border-radius: 3px;
        }
        
        #close {
            float: right;
            margin-right: 5px;
            background-color: red;
            color: white;
            font-weight: 900;
            border-radius: 5px;
            border: 0px;
            margin-top: 3px;
        }
        
        .inf-from {
            width: 100%;
            height: 600px;
            border-radius: 3px;
            overflow: scroll;
            padding: 10px;
        }
        
        .ph {
            text-align: left;
        }
    </style>
</head>

<body>
    <div style="width: 100%;height: 20px;"></div>
    <form class="form-inline" style="text-align: center;">
        <div class="form-group">
            <label for="orderID">订单id</label>
            <input type="text" class="form-control" id="orderID" placeholder="请输入订单ID">
            <label for="userID">用户id</label>
            <input type="text" class="form-control" id="userID" placeholder="请输入用户ID">
        </div>
        <button type="button" class="btn btn-default" onclick="findOrder()"><i class="iconfont"
                style="font-size: 14px;color: #3E8EF7;">&#xe660;</i></button>
        <button type="button" class="btn btn-info" onclick="flash()">刷新</button>
    </form>
    <div style="width: 100%;height: 20px;"></div>
    <table class="table table-bordered" style="text-align: center;">
        <thead id="th">
            <tr>
                <th><input type="checkbox" id="all"></th>
                <th>订单id(订单号)</th>
                <th>用户id</th>
                <th>购买时间</th>
                <th>订单状态</th>
                <th>地址</th>
                <th>价格</th>
                <th>操作</th>
            </tr>
        </thead>
        <tbody id="tb">

        </tbody>


    </table>
    <div style="float: right;margin-right: 20px;">
        <div style="float: left;height: 80px;line-height: 80px;margin-right: 20px;" id="pageNum">共&nbsp;<span style="color: red">${allOrders}</span>&nbsp;条记录&nbsp;&nbsp;</div>
        <div style="float: left;height: 80px;line-height: 80px;margin-right: 20px;">&nbsp;<span style="color: red">12</span>&nbsp;条&nbsp;/&nbsp;页</div>
        <div id="app" style="float: left;" onclick="page()">
            <el-pagination background layout="prev, pager, next" :total="${allPage}*10" style="margin-top: 23px;">
            </el-pagination>
        </div>
    </div>
    <div class="bg" style="position: fixed;">
        <div class="inf">
            <div class="inf-head">
                <h4 style="color: white;display: inline;line-height: 30px;margin-left: 5px;">订单详细信息</h4>
                <input type="button" id="close" value="×">
            </div>
            <div class="inf-from">
                <table class="table table-striped">
                    <caption>
                        <h3>购买的详细物品</h3>
                    </caption>
                    <thead>
                        <tr>
                            <th class="ph">商品id</th>
                            <th class="ph">商品类别</th>
                            <th class="ph">购买数量</th>
                            <th class="ph">状态(双击修改)</th>
                            <th class="ph">小计</th>
                        </tr>
                    </thead>
                    <tbody id="list_tb">

                    </tbody>
                </table>
            </div>
        </div>
    </div>
    <script type="text/javascript">
        new Vue().$mount('#app');

        let list_list = ${firstPage};
        let tb = document.getElementById("tb");
        let str = "";
        for (let i = 0; i < list_list.length; i++) {
            str += '<tr>';
            str += '<td><input type="checkbox" name="cks"></td>';
            str += '<td>' + list_list[i].buyId + '</td>';
            str += '<td>' + list_list[i].userId + '</td>';
            str += '<td>' + list_list[i].buyTime + '</td>';
            if(list_list[i].stateId==1){
                str += '<td>已支付</td>';
            }else {
                str += '<td>未支付</td>';
            }
            str += '<td>' + list_list[i].address + '</td>';
            str += '<td>' + list_list[i].totalMoney + '</td>';
            str += '<td>';
            str += '<button class="btn-link" type="button" onclick="search(this)">详情</button>';
            str += '</td>';
            str += '</tr>';
        }
        tb.innerHTML = str;

        function page() {
            let active = document.getElementsByClassName("active")[0];
            let param = "nowPage="+parseInt(active.innerText);
            $.get('/boot/admin/getNowOrders',param,function (res) {
                list_list = res;
                str = "";
                for (let i = 0; i < list_list.length; i++) {
                    str += '<tr>';
                    str += '<td><input type="checkbox" name="cks"></td>';
                    str += '<td>' + list_list[i].buyId + '</td>';
                    str += '<td>' + list_list[i].userId + '</td>';
                    str += '<td>' + list_list[i].buyTime + '</td>';
                    if(list_list[i].stateId==1){
                        str += '<td>已支付</td>';
                    }else {
                        str += '<td>未支付</td>';
                    }
                    str += '<td>' + list_list[i].address + '</td>';
                    str += '<td>' + list_list[i].totalMoney + '</td>';
                    str += '<td>';
                    str += '<button class="btn-link" type="button" onclick="search(this)">详情</button>';
                    str += '</td>';
                    str += '</tr>';
                }
                tb.innerHTML = str;
            })
        }

        function findOrder(){
            let buyId = document.getElementById("orderID").value;
            let userId = document.getElementById("userID").value;
            $.get('/boot/admin/findOrder',{buyId:buyId,userId:userId},function (res){
                str = "";
                if (res!=null&&res[0]!=null){
                    list_list = res;
                    for (let i = 0; i < list_list.length; i++) {
                        str += '<tr>';
                        str += '<td><input type="checkbox" name="cks"></td>';
                        str += '<td>' + list_list[i].buyId + '</td>';
                        str += '<td>' + list_list[i].userId + '</td>';
                        str += '<td>' + list_list[i].buyTime + '</td>';
                        if(list_list[i].stateId==1){
                            str += '<td>已支付</td>';
                        }else {
                            str += '<td>未支付</td>';
                        }
                        str += '<td>' + list_list[i].address + '</td>';
                        str += '<td>' + list_list[i].totalMoney + '</td>';
                        str += '<td>';
                        str += '<button class="btn-link" type="button" onclick="search(this)">详情</button>';
                        str += '</td>';
                        str += '</tr>';
                    }
                }else {
                    str += '<tr><td colspan="8">查询为空</td></tr>';
                }
                tb.innerHTML = str;
            })
        }


        function search(obj) {
            let bg = document.getElementsByClassName("bg");
            bg[0].style.display = "block";
            let buyId = obj.parentElement.parentElement.firstElementChild.nextElementSibling.innerText;
            $.get('/boot/admin/getOrderList',{buyId:buyId},function (res){
                let shop_list = res;
                let sum = 0;
                for (let i = 0; i < shop_list.length; i++) {
                    sum += shop_list[i].buyPrice;
                }
                let list_tb = document.getElementById("list_tb");
                let list_str = "";
                for (let i = 0; i < shop_list.length; i++) {
                    list_str += '<tr>';
                    list_str += '<td>' + shop_list[i].productId + '</td>';
                    if(shop_list[i].buyType==1){
                        list_str += '<td>实体书</td>';
                    }else if (shop_list[i].buyType==2){
                        list_str += '<td>电子书</td>';
                    }
                    list_str += '<td>' + shop_list[i].buyNum + '</td>';
                    if(shop_list[i].buyStatus==1){
                        list_str += '<td ondblclick="update(this,\'' + shop_list[i].id + '\')">待发货</td>';
                    }else if (shop_list[i].buyStatus==2){
                        list_str += '<td ondblclick="update(this,\'' + shop_list[i].id + '\')">待收货</td>';
                    }else if (shop_list[i].buyStatus==3){
                        list_str += '<td ondblclick="update(this,\'' + shop_list[i].id + '\')">已收货</td>';
                    }else if (shop_list[i].buyStatus==4){
                        list_str += '<td ondblclick="update(this,\'' + shop_list[i].id + '\')">退款申请中</td>';
                    }else if (shop_list[i].buyStatus==5){
                        list_str += '<td ondblclick="update(this,\'' + shop_list[i].id + '\')">退款成功</td>';
                    }
                    list_str += '<td>' + shop_list[i].buyPrice + '</td>';
                    list_str += '</tr>';
                }
                list_str += '<tr><td colspan="5" style="text-align: right;"><h4 style="color:red;">总价:' + sum + '</h4></td></tr>'
                list_tb.innerHTML = list_str;
            })
        }

        function update(td,id){
            if (parseInt(${admin.powerId})==1 || parseInt(${admin.powerId})==2){
                td.innerHTML = "";
                let select = document.createElement("select");
                select.innerHTML = "<option value='1'>待发货</option><option value='2'>待收货</option><option value='3'>已收货</option><option value='4'>退款申请中</option><option value='5'>退款成功</option>";
                select.onblur = function() {
                    td.innerHTML = this.options[this.selectedIndex].text;
                    let statusId = parseInt(this.options[this.selectedIndex].value);
                    if(confirm("你确定执行此操作吗?")) {
                        $.get('/boot/admin/updateOrderList', {id:id,statusId:statusId}, function (res) {
                            if (res.code=="200"){
                                alert(res.msg);
                            }else {
                                alert("修改失败");
                            }
                        })
                    }
                }
                td.appendChild(select);
            }else {
                alert("对不起!只有Ⅰ,Ⅱ级管理员才能执行此操作!!!")
            }
        }

        function flash(){
            location.reload();
        }

        $(function() {
            //通过点击最上面的全选框，它的选中状态决定后面复选框的选中状态
            $("#all").click(function() {
                console.log($(this).prop("checked"));
                console.log($("#tb input"));
                $("#tb input").prop("checked", $(this).prop("checked"));
            });

            //后面的复选框点击后，判断是否全部都选中，如果都选中，最上面的全选框自动勾上
            $("#tb input").click(function() {
                console.log($("#tb input:checked")); //选中的元素
                if ($("#tb input:checked").length == $("#tb input").length) {
                    $("#all").prop("checked", true);
                } else {
                    $("#all").prop("checked", false);
                }
            });
            $("#close").click(function() {
                $(".bg").css("display", "none");
            });
            $("#close-add").click(function() {
                $(".bg-add").css("display", "none");
            });

            $("#orderID").bind("input propertychange", function() {
                var text = $("#orderID").val();
                if (text != '') { //若输入框1的内容不为空，则输入框2不可用
                    $("#userID").attr("disabled", "disabled"); //设置输入框2不可用
                } else {
                    $("#userID").removeAttr("disabled"); //移除不可用的属性
                }
            })
            $("#userID").bind("input propertychange", function() {
                var text = $("#userID").val();
                if (text != '') { //若输入框1的内容不为空，则输入框2不可用
                    $("#orderID").attr("disabled", "disabled"); //设置输入框2不可用
                } else {
                    $("#orderID").removeAttr("disabled"); //移除不可用的属性
                }
            })
        });
    </script>
</body>

</html>